package com.lingtianyu.tech.erbaoshen.core.util.serialize;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Date;

@Slf4j
public class JacksonSerializeExecute implements SerializeExecute {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Object unSerializeArray(Object param, Type type) {
        Class<?> aClass = (Class<?>) type;
        try {
            return objectMapper.readValue(param.toString(), aClass);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public String unSerializeString(Object param) {
        return param.toString();
    }

    @Override
    public Date unSerializeDate(Object param) {
        try {
            return objectMapper.readValue(param.toString(), Date.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean unSerializeBoolean(Object param) {
        try {
            return objectMapper.readValue(param.toString(), boolean.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Character unSerializeChar(Object param) {
        try {
            return objectMapper.readValue(param.toString(), char.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Byte unSerializeByte(Object param) {
        try {
            return objectMapper.readValue(param.toString(), byte.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Short unSerializeShort(Object param) {
        try {
            return objectMapper.readValue(param.toString(), short.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer unSerializeInt(Object param) {
        try {
            return objectMapper.readValue(param.toString(), int.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long unSerializeLong(Object param) {
        try {
            return objectMapper.readValue(param.toString(), long.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Float unSerializeFloat(Object param) {
        try {
            return objectMapper.readValue(param.toString(), float.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Double unSerializeDouble(Object param) {
        try {
            return objectMapper.readValue(param.toString(), double.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public BigDecimal unSerializeBigDecimal(Object param) {
        try {
            return objectMapper.readValue(param.toString(), BigDecimal.class);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object unSerializeObject(Object param, Type type) {
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructType(type);
            return objectMapper.readValue(param.toString(), javaType);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object unSerializeDefault(Object param, Type type) {
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructType(type);
            return objectMapper.readValue(param.toString(), javaType);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public String serializeParam(Object param) {
        try {
            return objectMapper.writeValueAsString(param);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return "jackson";
    }
}
